var app=getApp()
Page({
  onLoad(e){
    this.setData({ order_id: e.order_id})
    this.chushi(); 
    let date = app.setdate(0);
    if(new Date().getHours()>=17){
      date = app.setdate(1);
    }
    let enddate = app.setdate(0);
    this.setData({ date: date, time:"09:00"});
    if(e.from&&e.from=='zhong'){
      this.setData({text:"重新预约"})
      let guihuanzhong=wx.getStorageSync('guihuanzhong');
      guihuanzhong = guihuanzhong.split(" ");
      let date = guihuanzhong[0];
      let time = guihuanzhong[1].split('~')[0];
      this.setData({olddate:date,oldtime:time})
    }
    if(e.code){
        this.setData({ code: e.code});
    }
//    console.info(e);
    //this.xiangqing();
  },
  data:{
    addressid: 0,//收货地址id
    address: {},
    date:"2018-2-1",
    time:'12:00',
    code:'JD',
    text:'预约快递'
  },
  bindDateChange(e){
    this.setData({ date:e.detail.value})
  },
  bindTimeChange(e){
    this.setData({ time:e.detail.value})
  },
  xiangqing(){
    let orderId = this.data.order_id;
    let token = wx.getStorageSync('token');
    let userId = wx.getStorageSync('userdata').user_id;
    let json = { token: token, userId: userId, orderId: orderId }
    app.request('index/order/orderfind', 'POST', json,'xiangqingb',this);
  },
  xiangqingb(data){
//    console.log(data);
  },
  chushi(){
    wx.showLoading();
    let order_id = this.data.order_id;
    let token = wx.getStorageSync('token');
    let user_id = wx.getStorageSync('userdata').user_id;
    let json={token:token,user_id:user_id,order_id:order_id}
    app.request('book/user/expressInitial','POST',json,'chushib',this)
  },
  chushib(data){//初始回调，下单时的数据
//    console.log(data);
    if(data.code!=200){wx.hideLoading(); return false}
    var $key = data.data.shipper.length - 1;
    for(var key in data.data.shipper){
        if(data.data.shipper[key].code === this.data.code){
            $key = key;
            break;
        }
    }
    this.setData({ shipper_code: data.data.shipper[$key].code,
    shipper_name: data.data.shipper[$key].name});
    if (data.data.sender_info.prev_express){//上次预约
      this.setData({ shipper_type: data.data.sender_info.prev_express.name})
    }
    this.oneaddress(data.data.sender_info.address_id);
  },
  oneaddress(id = '', moren = '0') {//收获地址
    let token = wx.getStorageSync('token');
    let user_id = wx.getStorageSync('userdata').user_id;
    let json = { token: token, userId: user_id, id: id, is_default: moren }
    app.request('index/User/userAddressInfo', 'POST', json, 'oneaddressb', this);
  },
  oneaddressb(data) {//地址回调
    wx.hideLoading();
    if (data.code != 200) {
      return false;
    }
    this.setData({ address: data.data.addressList, addressid: data.data.addressList.id });
  },
  checkaddress(e) {//跳到地址列表
    if (app.repeat(e.timeStamp) == false) { return; }
    let id = e.currentTarget.id;
    wx.navigateTo({ url: '/pages/home/addresslist/addresslist?id=' + id })
  },
  yuyue(){
    if (this.data.addressid==0){
      app.tishi('提示','请设置取件地址');
      return false;
    }
    let start_time = this.data.date + " " + this.data.time;;
    let _this=this;
    wx.showModal({
      title: '提示',
      content: '取件时间为' + start_time,
      confirmText:'是的',
      cancelText:'再想想',
      success(res){
        if(res.confirm){
          let token = wx.getStorageSync('token');
          let user_id = wx.getStorageSync('userdata').user_id;
          let order_id = _this.data.order_id;
          let shipper_code = _this.data.shipper_code;
          let address_id = _this.data.addressid;
          let json = {
            token: token, user_id: user_id, order_id: order_id, shipper_code: shipper_code,
            address_id: address_id, start_time: start_time
          }
          app.request('book/user/expressOrder', 'POST', json, "yuyueb", _this)
        }
      }
    })
    
  },
  yuyueb(data){
    if(data.code!=200){
      app.tishi('提示',data.msg)
      return false
    }
    wx.showModal({
      title: '提示',
      content: '预约成功，请在约定时间配合取件',
      showCancel:false,
      success(){
        let pages = getCurrentPages();//页面栈
        var prevPage = pages[pages.length - 2];//上一页Page对象
        prevPage.setData({ lists: [], page: 1 })
        prevPage.getorder();
        wx.navigateBack({ delta: 1 })
      }
    })  
  },
  editExpress:function(){
      var page = getCurrentPages().pop(),url = page.route,params = [];
      for(var key in page.options){
          params.push(key + '=' + page.options[key]);
      }
      wx.navigateTo({url:'/pages/index/express/express?url=' + encodeURIComponent('/' + url + '?' + params.join('&'))});
  }
})